; jQuery(function() {

	formValidate();
	initTheme();
	ajaxInterceptor();
	// 停止
	$("body").on('click', '[data-stopPropagation]', function(e) {
		e.stopPropagation();
	});

	// 滚动条
	if ($('.lyear-scroll')[0]) {
		$('.lyear-scroll').each(function() {
			new PerfectScrollbar(this, {
				swipeEasing: false,
				suppressScrollX: true
			});
		});
	}




	function initTheme() {
		// 读取cookie中的主题设置
		var the_logo_bg = $.cookie('the_logo_bg'),
			the_header_bg = $.cookie('the_header_bg'),
			the_sidebar_bg = $.cookie('the_sidebar_bg'),
			the_site_theme = $.cookie('the_site_theme');

		if (the_logo_bg) $('body').attr('data-logobg', the_logo_bg);
		if (the_header_bg) $('body').attr('data-headerbg', the_header_bg);
		if (the_sidebar_bg) $('body').attr('data-sidebarbg', the_sidebar_bg);
		if (the_site_theme) {
			$('body').attr('data-theme', the_site_theme);
		}

		// 处理主题配色下拉选中
		$(".dropdown-skin :radio").each(function() {
			var $this = $(this),
				radioName = $this.attr('name');
			switch (radioName) {
				case 'site_theme':
					$this.val() == the_site_theme && $this.prop("checked", true);
					break;
				case 'logo_bg':
					$this.val() == the_logo_bg && $this.prop("checked", true);
					break;
				case 'header_bg':
					$this.val() == the_header_bg && $this.prop("checked", true);
					break;
				case 'sidebar_bg':
					$this.val() == the_sidebar_bg && $this.prop("checked", true);
			}
		});
		setTheme('site_theme', 'data-theme');
		setTheme('sidebar_bg', 'data-sidebarbg');
		setTheme('logo_bg', 'data-logobg');
		setTheme('header_bg', 'data-headerbg');
	}

	// 设置主题配色
	function setTheme(input_name, data_name) {
		$("input[name='" + input_name + "']").click(function() {
			var check_theme = $(this).val();
			$('body').attr(data_name, check_theme);
			$.cookie('the_' + input_name, $(this).val());
		});
	}
	/**
 * 修改校验提示位置，与bootstrap validate 一致
 */
	function formValidate() {
		// 手机验证   
		$.validator.addMethod("phone", function(value, element) {
			var tel = /^1[3-9]\d{9}$/;
			return this.optional(element) || (tel.test(value));
		}, "请正确填写您的手机号码");

		$.validator.setDefaults({
			errorElement: "div",
			ignore: "[novalidate]",
			errorPlacement: function(error, element) {
				// Add the `invalid-feedback` class to the error element
				error.addClass("invalid-feedback");
				if (element.prop("type") === "checkbox") {
					error.insertAfter(element.siblings("label"));
				}
				else {
					error.insertAfter(element);
				}
			}
		});

		$("form").validate();
	}

	/**
	 * ajax 拦截
	 */
	function ajaxInterceptor() {
		$(document).ajaxSend(function(event, xhr, settings) {
			// 修改请求参数
			xhr.setRequestHeader('token', sessionStorage.getItem('token'));
		});

		// 响应成功后的拦截器
		$(document).ajaxSuccess(function(event, xhr, settings, data) {
			// 修改响应数据
			if (isJSON(data)) {
				var json = JSON.parse(data);
				switch (json.code) {
					case 3:
					case 4:
						sessionStorage.clear();
						window.location.href = "/login.html";
						break;
					case 5:
						admin.startPage( "view/exception/403.html", "403");
				}
			}
		});

		$(document).ajaxError(function(event, xhr, options, exc) {
			if (xhr.status == 404) {
				admin.startPage( "view/exception/404.html", "404");
			} else if (xhr.status == 500) {
				admin.startPage( "view/exception/500.html", "500");
			}
		})
	}

	function isJSON(str) {
		try {
			JSON.parse(str);
			return true;
		} catch (error) {
			return false;
		}
	}
});